package com.ifreeee.practice.pc.famousWorks;

import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.Mp3File;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class Mp3Title {

    private String oldPath;

    private String newPath;

    private String childPath = "processed";

    public Mp3Title(String oldPath) {
        this.oldPath = oldPath;
        if (!StringUtils.endsWith(this.oldPath, File.separator)) {
            this.oldPath = this.oldPath + File.separator;
        }
        this.newPath = this.oldPath + this.childPath + File.separator;
    }

    public Mp3Title(String oldPath, String childPath) {
        this.oldPath = oldPath;
        this.childPath = childPath;
        if (!StringUtils.endsWith(this.oldPath, File.separator)) {
            this.oldPath = this.oldPath + File.separator;
        }
        this.newPath = this.oldPath + this.childPath + File.separator;
    }

    public static void main(String[] path) throws Exception {
        Mp3Title mp3Title = new Mp3Title("D:\\repository\\名著");
        mp3Title.setTitle();
    }

    public void setChildPath(String childPath) {
        this.childPath = childPath;
        this.newPath = this.oldPath + this.childPath + File.separator;
    }

    public void setTitle() {
        File file = new File(oldPath);
        if (!file.exists()) {
            return;
        }
        if (file.isFile()) {
            String currDir = file.getParent();
            if (!StringUtils.endsWith(currDir, File.separator)) {
                currDir = currDir + File.separator;
            }
            String newPath = currDir + this.childPath + File.separator + file.getName();
            try {
                setTitle(oldPath, newPath);
            } catch (Exception exception) {
                System.out.println(exception.getMessage());
            }
            return;
        }
        if (file.isDirectory()) {
            handleDir(file);
        }
    }

    /**
     * 层层递归处理目录下的 所有 mp3 文件
     * @param dir
     */
    private void handleDir (File dir) {
        if (!dir.isDirectory()) {
            return;
        }
        for (File file : dir.listFiles()) {
            String fileStr = file.getAbsolutePath();
            if (!file.exists()) {
                continue;
            }
            if (file.isDirectory()) {
                handleDir(file);
            }
            if (file.isFile()) {
                try {
                    setTitle(fileStr, fileStr.replace(this.oldPath, this.newPath));
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
    }

    /**
     * 将 该 mp3 文件标题进行修改成该文件的文件名
     * @param filePath
     * @param newPath
     * @throws Exception
     */
    private void setTitle(String filePath, String newPath) throws Exception {
        File file = new File(filePath);
        if (!file.exists() || !file.isFile() || !filePath.endsWith(".mp3")) {
            return;
        }
        Mp3File mp3file = new Mp3File(filePath);
        if (!mp3file.hasId3v2Tag()) {
            return;
        }
        String filename = file.getName();
        ID3v2 id3v2Tag = mp3file.getId3v2Tag();
        id3v2Tag.setTitle(filename);
        File newFile = new File(newPath);
        if (newFile.exists()) {
            newFile.delete();
        }
        File dir = new File(newFile.getParent());
        if (!dir.isDirectory()) {
            dir.delete();
        }
        if (!dir.exists()) {
            dir.mkdirs();
        }
        mp3file.save(newPath);
    }

}
